package com.aode.playlist.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.aode.common.bean.request.Request;
import com.aode.common.bean.response.Mllist;
import com.aode.common.bean.web.Result;
import com.aode.playlist.R;
import com.aode.playlist.adapter.MusicAdapter;
import com.aode.playlist.adapter.PlayListAdapter;
import com.aode.playlist.databinding.ActivityPlaylistBinding;
import com.aode.playlist.viewmodel.HomePlayListViewModel;
import com.aode.playlist.viewmodel.PlayListViewModel;

import java.util.ArrayList;
import java.util.List;

public class PlayListActivity extends AppCompatActivity implements PlayListAdapter.MllistInterface {
    private ActivityPlaylistBinding playlistBinding;
    private PlayListViewModel playListViewModel;
    private List<Mllist> mllistList;
    private PlayListAdapter playListAdapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        playlistBinding = DataBindingUtil.setContentView(this, R.layout.activity_playlist);
        playListViewModel = ViewModelProviders.of(this).get( PlayListViewModel.class);
        initData();
        initListener();
    }

    private void initListener() {
        playListViewModel.getPlaylistLiveData().observe(this, listResult -> {
            if(listResult.getCode().equals("0")){
                if(!listResult.getData().isEmpty()&&listResult.getData()!=null&&listResult.getData().size()>0){
                    mllistList = listResult.getData();
                    initRecycler((ArrayList<Mllist>) mllistList);
                }
            }
        });
    }

    private void initRecycler(ArrayList<Mllist> mllistList) {
        if(mllistList!=null&&!mllistList.isEmpty()&&mllistList.size()>0){
            playListAdapter = new PlayListAdapter(this,mllistList,this);
            playlistBinding.mllistRecycler.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
            playlistBinding.mllistRecycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
            playlistBinding.mllistRecycler.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.HORIZONTAL));
            playlistBinding.mllistRecycler.setAdapter(playListAdapter);
        }
    }

    private void initData() {
        playListViewModel.getPlayList();
    }


    @Override
    public void onItemClick(Mllist mllist) {
        Log.e("TAG",mllist.toString());
        Intent intent = new Intent();
        intent.setClass(PlayListActivity.this, HomePlayListActivity.class);
        Bundle bundle = new Bundle();
        bundle.putParcelable("mllist",mllist);
        intent.putExtras(bundle);
        startActivityForResult(intent, Request.PLAY_TO_PLAYLIST);
    }
}
